<?php
namespace app\admin\controller;

use app\common\model\Member as MemberModel;
use app\common\model\MemberLevel as LevelModel;
/**
 * 会员控制器
 */
class Member extends Admin 
{
	/**
	 * 会员列表
	 * @return array
	 */
	public function index(){
		$key = $this->request->param('key/s');
		$status = $this->request->param('status/d');

		$where = [];
		$map = [];
		$query = [];
		if(!empty($key)){
			$where[] = ['nickname|realname|username','like', '%'.$key.'%'];
			$query['key'] = $key;
		}

		if(!empty($status)){
			$map['status'] = $status;
			$query['status'] = $status;
		}

		$list = MemberModel::where($map)->where($where)->order('id desc')->paginate(14, false, ['query' => $query]);

		$this->assign('list', $list);

		return $this->fetch();
	}

	/**
	 * 添加会员
	 */
	public function add(){
		if($this->request->isPost()){
			$data = $this->request->post();
            // 验证
            $result = $this->validate($data, [
                'username|用户名' => 'require|alphaNum|unique:member',
        		'password|密码'  => 'require|length:6,20',
        		'repassword|确认密码'  => 'require|confirm:password',
				'mobile|手机号'   => 'regex:^1\d{10}'
            ]);
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);
            unset($data['repassword']);
            if(empty($data['password'])){
				unset($data['password']);
			}

			if($res = MemberModel::create($data)){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}else{
			$level = LevelModel::where('status',1)->select();

			$this->assign('title', '新增会员');
			$this->assign('info', null);			
			$this->assign('level', $level);

			return $this->fetch('edit');
		}
	}

	/**
	 * 编辑会员
	 */
	public function edit($id = 0){
		if($this->request->isPost()){
			$data = $this->request->post();
			// 验证
            $result = $this->validate($data, [
        		'password|密码'  => 'length:6,20',        		
				'mobile|手机号'   => 'regex:^1\d{10}'
            ]);
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);
            unset($data['repassword']);
            if(empty($data['password'])){
				unset($data['password']);
			}

			if($res = MemberModel::update($data)){
				$this->success('编辑成功');
			}else{
				$this->error('编辑失败');
			}
		}else{
			empty($id) && $this->error('参数错误');

			$info = MemberModel::where('id',$id)->find();
			$level = LevelModel::where('status',1)->select();
			$this->assign('title', '编辑会员');
			$this->assign('info', $info);
			$this->assign('level', $level);

			return $this->fetch();
		}
	}

	/**
	 * 删除会员
	 */
	public function del($id = 0){
		empty($id) && $this->error('参数错误');

		if(is_array($id)){
			$map[] = ['id','in',$id];
		}elseif(is_numeric($id)){
			$map[] = ['id','=',$id];
		}

		if($query = MemberModel::where($map)->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 更新状态
	 * @return mixed
	 */
	public function update(){
		$data = $this->request->param();

		if(empty($data)){
			$this->error('参数错误');
		}	
		
		if($query = MemberModel::update($data)){
			$this->success('更新成功');
		}else{
			$this->error('更新失败');
		}
	}

	/**
	 * 会员等级
	 */
	public function level(){
		$key = $this->request->param('key/s');
		$status = $this->request->param('status/d');

		$where = [];
		$map = [];
		$query = [];
		if(!empty($key)){
			$where[] = ['levelname','like','%'.$key.'%'];
			$query['key'] = $key;
		}

		if(!empty($status)){
			$map['status'] = $status;
			$query['status'] = $status;
		}
		
		$list = LevelModel::where($map)->where($where)->order('id desc')->paginate(15, false, ['query' => $query]);

		$this->assign('list', $list);
		return $this->fetch();
	}

	/**
	 * 添加会员等级
	 */
	public function addLevel(){
		if($this->request->isPost()){
			$data = $this->request->post();
			// 验证
            $result = $this->validate($data, [
        		'name|等级名称'  => 'require'
            ]);
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            if($res = LevelModel::create($data)){
            	$this->success('新增成功');
            }else{
            	$this->error('新增失败');
            }
		}else{
			$this->assign('title','新增等级');
			$this->assign('info', null);
			return $this->fetch('editLevel');
		}
	}

	/**
	 * 编辑会员等级
	 */
	public function editLevel($id = 0){
		if($this->request->isPost()){
			$data = $this->request->post();
			// 验证
            $result = $this->validate($data, [
        		'name|等级名称'  => 'require'
            ]);
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            if($res = LevelModel::update($data)){
            	$this->success('编辑成功');
            }else{
            	$this->error('编辑失败');
            }
		}else{
			empty($id) && $this->error('参数错误');

			$info = LevelModel::where('id',$id)->find();
			$this->assign('title','编辑等级');
			$this->assign('info', $info);

			return $this->fetch('editLevel');
		}
	}

	/**
	 * 删除会员
	 */
	public function delLevel($id = 0){
		empty($id) && $this->error('参数错误');

		if(is_array($id)){
			$map[] = ['id','in',$id];
		}elseif(is_numeric($id)){
			$map[] = ['id','=',$id];
		}

		if($query = LevelModel::where($map)->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 更新状态
	 * @return mixed
	 */
	public function updateLevel(){
		$data = $this->request->param();

		if(empty($data)){
			$this->error('参数错误');
		}	
		
		if($query = LevelModel::update($data)){
			$this->success('更新成功');
		}else{
			$this->error('更新失败');
		}
	}
}